package text.ui;

import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.filechooser.FileFilter;

public class TExtFileFilter extends FileFilter {
	Hashtable extensions;
	
	public TExtFileFilter() {
		extensions = new Hashtable(3);
	}

	public String getDescription(){
		Enumeration ee = extensions.keys();
		StringBuffer desc = new StringBuffer("Files (");
		
		while(ee.hasMoreElements()) {
			desc.append("."+ee.nextElement()+ " ");
		}
		desc.append(")");
		return desc.toString();
	}
	
	public boolean accept(File f){
		if(f != null) {
		    if(f.isDirectory()) {
				return true;
		    }
		    String extension = getExtension(f);
		    if(extension != null && extensions.get(extension) != null) {
				return true;
		    }
		}
		return false;
	}

	public void addExtension(String extension) {
		extensions.put(extension.toLowerCase(), this);
	}

	private String getExtension(File f) {
		String s = f.getName();
		int i = s.lastIndexOf(".");
		if (i>0 && i< s.length()-1) {
			return s.substring(i+1).toLowerCase();
		}
		return null;
	}

	public File appendExtension(File f, String ext){
		String s = f.toString()+ext;
		return new File(s);
	}
	
}